<template>
	<view class="content1">
		<view class="content2">
			<u-button type="primary" text="获取并计算平均分" @click="getFen"></u-button>
			<u-cell-group style="width: 100%;">
				<u-cell title="所有分数" :label="allfen"></u-cell>
				<u-cell title="最高分" :value="result.max"></u-cell>
				<u-cell title="最低分" :value="result.min"></u-cell>
			</u-cell-group>
			<text class="pingjunfen">平均分：<text class="yanse">{{pingjunfen.toFixed(2)}}</text>分</text>
		</view>
		<view style="margin-bottom: 50rpx;display: flex;">
			<u-button type="warning" :text="showKaiguan?'禁止投票':'允许投票'" @click="isToupiao"
				style="margin-right: 20rpx;"></u-button>
			<u-button type="error" text="重置分数" @click="clearFen"></u-button>

		</view>
		<u-loading-page :loading="loading" loading-text="加载中..." color="#fff" bg-color="#92c7e8"></u-loading-page>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				pingjunfen: 0,
				result: {},
				allfen: '',
				showKaiguan: 0
			};
		},
		onLoad() {
			this.getKaiGuan()
		},
		methods: {
			isToupiao() {
				this.loading = true
				uniCloud.callFunction({
					name: 'changekaiguan',
					data: {
						kaiguan: this.showKaiguan ? 0 : 1
					},
					success: (res) => {
						console.log(res)
						this.getKaiGuan()
					},
					complete: () => {
						this.loading = false
					}
				})
			},
			getKaiGuan() {
				this.loading = true
				uniCloud.callFunction({
					name: 'getkaiguan',
					success: (res) => {
						console.log('开关状态', res)
						this.showKaiguan = res.result.data[0].kaiguan
					},
					complete: () => {
						this.loading = false
					}
				})
			},
			clearFen() {
				this.loading = true
				uniCloud.callFunction({
					name: 'clearfenshu',
					success: (res) => {
						console.log(res)
					},
					complete: () => {
						this.loading = false
					}
				})
			},
			getFen() {
				this.loading = true
				uniCloud.callFunction({
					name: 'getfenshu',
					success: (res) => {
						console.log(res)
						let arr = []
						res.result.data.forEach(res1 => {
							arr.push(res1.fen)
						})
						this.allfen = arr.join('、')
						this.pingjunfen = this.calculateAverageWithoutExtremes(res.result.data)
						console.log('平均分', this.pingjunfen)
					},
					complete: () => {
						this.loading = false
					}
				})
			},
			calculateAverageWithoutExtremes(scores) {
				// 首先确保数组至少有3个元素，因为我们需要去掉一个最高分和一个最低分
				if (!Array.isArray(scores) || scores.length <= 2) {
					uni.showToast({
						title: '至少需要三个分数来计算平均值。',
						icon: 'none',
						mask: true
					})
					throw new Error("至少需要三个分数来计算平均值。");
				}

				// 使用Array.prototype.reduce方法求得总分和最高分和最低分
				const result = scores.reduce((acc, scoreObj) => {
					const score = scoreObj.fen;
					acc.total += score;
					acc.max = Math.max(acc.max, score);
					acc.min = Math.min(acc.min, score);
					return acc;
				}, {
					total: 0,
					max: -Infinity,
					min: Infinity
				});
				this.result = result
				// 计算平均分，去掉最高和最低分后：
				const average = (result.total - result.max - result.min) / (scores.length - 2);

				return average;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pingjunfen {
		font-size: 48rpx;
		line-height: 60rpx;
		margin: 40rpx auto;

	}

	.yanse {
		font-weight: bold;
		font-size: 58rpx;
		line-height: 60rpx;
		color: $uni-color-error;
	}

	.content2 {
		display: flex;
		flex-direction: column;
	}

	.content1 {
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 30rpx;
	}
</style>